﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using ShopOnline.Models;
using ShopOnline.Models.Utils;

namespace ShopOnline.Controllers
{
    public class CatelogyController : Controller
    {
        private DAL_Catelogy CatelogyRepositpry;
        private static int catelogyIdCurent=0;
        private ShopOnlineDBDataContext db;
        public static int CatelogyIdCurent
        {
            get { return CatelogyController.catelogyIdCurent; }
            set { CatelogyController.catelogyIdCurent = value; }
        }
        public CatelogyController()
        {
            CatelogyRepositpry = new DAL_Catelogy();
            db = new ShopOnlineDBDataContext();
        }
        //
        // GET: /Catelogy/
    [Authorize]
        public ActionResult AdminListCatelogy()
        {
            var listcate = CatelogyRepositpry.getListCatelogy();
            return View(listcate);
        }

        //
        // GET: /Catelogy/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            var catelogy = CatelogyRepositpry.getCatelogyById(id);
            return View(catelogy);
        }

        //
        // GET: /Catelogy/Create
         [Authorize]
        public ActionResult Create()
        {
            var listCatelogy = CatelogyRepositpry.getListCatelogy();
            Catelogy cateroot = new Catelogy();
            cateroot.catelogyId = 0;
            cateroot.catelogyName = "Root";
            listCatelogy.Add(cateroot);
            ViewData["listCatelogy"] = listCatelogy;
            return View();
        } 

        //
        // POST: /Catelogy/Create
         [Authorize]
        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            try
            {
                Catelogy catelogy = new Catelogy();
                catelogy.body = form["body"];
                catelogy.catelogyName = form["catelogyName"];
                catelogy.description = form["description"];
                catelogy.curent = true;
                var show = form["show"];
                if (show=="false")
                {
                    catelogy.show = false;
                }
                else
                {
                    catelogy.show = true;
                }
                var enableModifyChirldren = form["enableModifyChirldren"];
                if (enableModifyChirldren=="false")
                {
                    catelogy.enableModifyChirldren = false;
                }
                else
                {
                    catelogy.enableModifyChirldren = true;
                }
                
                catelogy.piority=int.Parse(form["piority"]);
                catelogy.url= form["url"];
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\CatelogyImages");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                catelogy.imagePath = uploader.PostedFile.FileName;
                int fartherId = int.Parse(form["fatherId"]);
                ShopOnline.Models.ShopOnlineDBDataContext DB = new ShopOnlineDBDataContext();
                if (fartherId==0)
                {
                    DB.Catelogies_add(catelogy.catelogyName, catelogy.description, catelogy.body, catelogy.piority, catelogy.url, catelogy.imagePath, false, true, true, 0);
                }
                else
                {
                    DB.Catelogies_add(catelogy.catelogyName, catelogy.description, catelogy.body, catelogy.piority, catelogy.url, catelogy.imagePath, false, true, true, fartherId);
                }
               
                return RedirectToAction("AdminListCatelogy");
            }
            catch
            {
                var listCatelogy = CatelogyRepositpry.getListCatelogy();
                Catelogy cateroot = new Catelogy();
                cateroot.catelogyId = 0;
                cateroot.catelogyName = "Root";
                listCatelogy.Add(cateroot);
                ViewData["listCatelogy"] = listCatelogy;
                return View();
            }
        }
        
        //
        // GET: /Catelogy/Edit/5
  [Authorize]
        public ActionResult Edit(int id)
        {
            try
            {
                      var catelogy = CatelogyRepositpry.getCatelogyById(id);          
                    var listCatelogy = CatelogyRepositpry.getListCatelogy();
                    Catelogy cateroot = new Catelogy();
                    cateroot.catelogyId = 0;
                    cateroot.catelogyName = "Root";
                    listCatelogy.Add(cateroot);
                    ViewData["listCatelogy"] = listCatelogy;
                    return View(catelogy);    
            }
            catch (Exception)
            {
                return RedirectToAction("AdminListCatelogy");
            }
        }

        //
        // POST: /Catelogy/Edit/5
 [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection form)
        {
            try
            {
                Catelogy catelogy = CatelogyRepositpry.getCatelogyById(id);
                catelogy.body = form["body"];
                catelogy.catelogyName = form["catelogyName"];
                catelogy.description = form["description"];
                catelogy.curent = false;
                var show = form["show"];
                catelogy.show = true;
                if (show=="false")
                {
                    catelogy.show = false;
                }
                var enableModifyChirldren = form["enableModifyChirldren"];
                catelogy.enableModifyChirldren = true;
                if (enableModifyChirldren=="false")
                {
                    catelogy.enableModifyChirldren = false;
                }
                catelogy.piority = int.Parse(form["piority"]);
                catelogy.url = form["url"];

                int fartherId = int.Parse(form["fatherId"]);
                ShopOnline.Models.ShopOnlineDBDataContext DB = new ShopOnlineDBDataContext();
                if (fartherId == 0)
                {
                    DB.Catelogies_update(catelogy.catelogyId,catelogy.catelogyName, catelogy.description, catelogy.body, catelogy.piority, catelogy.url, catelogy.imagePath, false,catelogy.show, catelogy.enableModifyChirldren, 0);
                }
                else
                {
                    DB.Catelogies_update(catelogy.catelogyId, catelogy.catelogyName, catelogy.description, catelogy.body, catelogy.piority, catelogy.url, catelogy.imagePath, false, catelogy.show, catelogy.enableModifyChirldren, fartherId);
                }

                return RedirectToAction("AdminListCatelogy");
            }
            catch
            {
                var catelogy = CatelogyRepositpry.getCatelogyById(id);
                var listCatelogy = CatelogyRepositpry.getListCatelogy();
                Catelogy cateroot = new Catelogy();
                cateroot.catelogyId = 0;
                cateroot.catelogyName = "Root";
                listCatelogy.Add(cateroot);
                ViewData["listCatelogy"] = listCatelogy;
                return View(catelogy); 
            }
        }

        //
        // GET: /Catelogy/Delete/5
  [Authorize]
        public ActionResult Delete(int id)
        {
            var catelogy = CatelogyRepositpry.getCatelogyById(id);
            return View(catelogy);
        }

        //
        // POST: /Catelogy/Delete/5
 [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var catelogy = CatelogyRepositpry.getCatelogyById(id);
                CatelogyRepositpry.DeleteCatelogy(catelogy);
                return RedirectToAction("AdminListCatelogy");
            }
            catch
            {
                var catelogy = CatelogyRepositpry.getCatelogyById(id);
                return View(catelogy);
            }
        }

        public ActionResult MenuLeft()
        {
            var ListMenuItem = CatelogyRepositpry.getListMenuItem();
            return View(ListMenuItem);
        }
 [Authorize]
        public ActionResult EditImage(int id)
        {
            catelogyIdCurent = id;
            var catelogy = CatelogyRepositpry.getCatelogyById(id);
            return View(catelogy);
        }
         [Authorize]
         [HttpPost]
        public ActionResult EditImages()
        {
            try
            {
                var catelogy = CatelogyRepositpry.getCatelogyById(catelogyIdCurent);
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\CatelogyImages");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                string abc = uploader.PostedFile.FileName;
                db.Catelogies_update(catelogy.catelogyId, catelogy.catelogyName, catelogy.description, catelogy.body, catelogy.piority, catelogy.url,abc, false, catelogy.show, catelogy.enableModifyChirldren, 0);
                return RedirectToAction("Edit", new { id = catelogyIdCurent });
            }
            catch (Exception)
            {
                return RedirectToAction("EditImage", new { id = catelogyIdCurent });
            } 
        }
    }
}
